package main

import (
	"fmt"
	"net/http"

	"github.com/PuerkitoBio/goquery"
)

// 发送请求 ==> 解析网页 ==> 获取结点 ==> 保存信息

func main() {
	Spider()
}

// 爬虫客户端
func Spider() {
	// 1.发送请求
	client := http.Client{}
	req, err := http.NewRequest("GET", "https://movie.douban.com/top250", nil)
	if err != nil {
		fmt.Println("req err", err)
	}
	// 伪造成浏览器访问，加一些请求头
	req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
	// req.Header.Set("Accept-Encoding", "gzip, deflate, br")
	req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
	req.Header.Set("Connection", "keep-alive")
	req.Header.Set("Cache-Control", "max-age=0")
	req.Header.Set("Host", "movie.douban.com")
	req.Header.Set("DNT", "1")
	req.Header.Set("sec-ch-ua", "\"Microsoft Edge\";v=\"105\", \"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"105\"")
	req.Header.Set("sec-ch-ua-platform", "Windows")
	req.Header.Set("sec-ch-ua-mobile", "?0")
	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.53")
	req.Header.Set("Upgrade-Insecure-Requests", "1")
	req.Header.Set("Sec-Fetch-User", "?1")
	req.Header.Set("Sec-Fetch-Site", "none")
	req.Header.Set("Sec-Fetch-Mode", "navigate")
	req.Header.Set("Sec-Fetch-Dest", "document")

	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("req failure", err)
	}
	defer resp.Body.Close()
	// 2.解析网页
	docDetail, err := goquery.NewDocumentFromReader(resp.Body)
	if err != nil {
		fmt.Println("xpath failure", err)
	}
	// 3.获取结点信息
	// #content > div > div.article > ol > li:nth-child(1) > div > div.info > div.hd > a > span:nth-child(1)
	// title := docDetail.Find("#content > div > div.article > ol > li:nth-child(1) > div > div.info > div.hd > a > span:nth-child(1)").Text()
	// fmt.Println("title is", title)

	// 获取全部的li标签
	// #content > div > div.article > ol > li:nth-child(1)
	docDetail.Find("#content > div > div.article > ol > li"). // 列表
									Each(func(i int, s *goquery.Selection) { // 在列表里继续找
			// 标题
			title := s.Find("div > div.info > div.hd > a > span:nth-child(1)").Text()
			// 图
			imgSec := s.Find("div > div.pic >  a > img") // img标签 --> src属性
			img, ok := imgSec.Attr("src")
			// 详情
			info := s.Find("div > div.info > div.bd > p:nth-child(1)").Text()
			// 分数
			score := s.Find("div > div.info > div.bd > div > span.rating_num").Text()
			// “废话”
			quote := s.Find("div > div.info > div.bd > p.quote > span").Text()

			if ok {
				fmt.Println("title:", title)
				fmt.Println("img:", img)
				fmt.Println("info:", info)
				fmt.Println("score:", score)
				fmt.Println("quote:", quote)
			}
		})
}
